package jmu.cyxwcy.librarymanagement.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import jmu.cyxwcy.librarymanagement.pojo.Book;
import jmu.cyxwcy.librarymanagement.pojo.BookDescription;
import jmu.cyxwcy.librarymanagement.pojo.Library;
import jmu.cyxwcy.librarymanagement.pojo.Result;
import jmu.cyxwcy.librarymanagement.service.BookService;
import jmu.cyxwcy.librarymanagement.service.ILibraryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 
 * @since 2024-06-24
 */
@RestController
@RequestMapping("/library")
public class LibraryController {
    @Autowired
    private ILibraryService iLibraryService;

    @PostMapping("/add")
    public Result addLibrary(@RequestBody Library library){
        iLibraryService.save(library);
        return Result.success();
    }

    @PutMapping("/update")
    public Result updateLibrary(@RequestBody Library library){
        iLibraryService.updateById(library);
        return Result.success();
    }

    @DeleteMapping("/delete")
    public Result deleteLibrary(Integer libraryId){
        iLibraryService.removeById(libraryId);
        return Result.success();
    }

    @GetMapping("/page")
    public Result<IPage<Library>> findPage(@RequestParam Integer pageNum,
                                @RequestParam Integer pageSize,
                                @RequestParam(defaultValue = "") String libraryName) {
        IPage<Library> page = new Page<>(pageNum, pageSize);
//      QueryWrapper<Book> queryWrapper = new QueryWrapper<>();
        QueryWrapper<Library> queryWrapper = new QueryWrapper<>();
        queryWrapper.like(!libraryName.isEmpty(), "library_name",libraryName);
        queryWrapper.orderByAsc("library_id");
        IPage<Library> pg= iLibraryService.page(page, queryWrapper);
        return Result.success(pg);
    }

}
